package excercise.recursive;

import java.util.ArrayList;
import java.util.List;

/**
 * String s = "abcdef"; 全排列
 */
public class Question1 {
    public List<String> solution(String str) {
        char[] arr = str.toCharArray();
        boolean[] used = new boolean[str.length()];
        List<String> r = new ArrayList<>();
        f(arr, used, new ArrayList<>(), r);

        r.forEach(System.out::println);

        return r;
    }

    public void f(char[] arr, boolean[] used, List<String> path, List<String> r) {

        if (path.size() == arr.length) {
            StringBuilder s = new StringBuilder();
            path.forEach(s::append);
            r.add(s.toString());
        }

        for (int i = 0; i < arr.length; i++) {
            if (used[i]) {
                continue;
            }
            path.add(String.valueOf(arr[i]));
            used[i] = true;
            f(arr, used, path, r);
            path.remove(path.size() - 1);
            used[i] = false;
        }

    }

    public static void main(String[] args) {
        new Question1().solution("abc");
    }
}
